package com.dl.test.util.file;

import com.jcraft.jsch.*;
import lombok.SneakyThrows;

import java.io.*;
import java.util.Properties;
import java.util.Vector;

/**
 * @author DL
 * @create 2022-10-26 16:36
 */
public class UploadUtils {

    private static final String host = "192.168.142.101";

    private static final int port = 22;

    private static final String username = "root";

    private static final String password = "123626";

    /**
     *
     * @param localPath 文件全路径
     * @param remotePath 上传到目的端目录
     */
    public static void uploadFile(String localPath, String remotePath) {
        FileInputStream input = null;
        ChannelSftp sftp = null;
        try {
            JSch jsch = new JSch();
            // 获取session  账号-ip-端口
            Session sshSession = jsch.getSession(username, host, port);
            // 添加密码
            sshSession.setPassword(password);
            Properties sshConfig = new Properties();
            // 严格主机密钥检查
            sshConfig.put("StrictHostKeyChecking", "no");
            sshSession.setConfig(sshConfig);
            // 开启session连接
            sshSession.connect();
            // 获取sftp通道
            sftp = (ChannelSftp) sshSession.openChannel("sftp");
            // 开启
            sftp.connect();
            //判断目录是否存在
            try {
                sftp.ls(remotePath); // 判断是否存在路径
            } catch (SftpException e) {
                System.out.println("不存在路径，进行创建");
                sftp.mkdir(remotePath);
            }
            sftp.cd(remotePath);
            String filename = localPath.substring(localPath.lastIndexOf(File.separator) + 1); //附件名字
            //filename = new String(filename.getBytes("GBK"), StandardCharsets.ISO_8859_1);
            input = new FileInputStream(localPath);
            sftp.put(input, filename);
            //设定777权限，转为8进制放入chmod中
            sftp.chmod(Integer.parseInt("777", 8), remotePath + filename);
            input.close();
            sftp.disconnect();
            sshSession.disconnect();
            System.out.println("================上传成功！==================");
        } catch (Exception e) {
            System.out.println("================上传失败！==================");
            e.printStackTrace();
        }
    }

    /**
     * @param remotePath    SFTP服务器的文件路径
     * @param downloadFileName SFTP服务器上的文件名
     * @param localPath     保存到本地路径
     */
    @SneakyThrows
    public static void downloadFile(String remotePath, String downloadFileName, String localPath) {
        ChannelSftp sftp = null;
        try {
            JSch jsch = new JSch();
            Session sshSession = jsch.getSession(username, host, port);
            sshSession.setPassword(password);
            Properties sshConfig = new Properties();
            // 严格主机密钥检查
            sshConfig.put("StrictHostKeyChecking", "no");
            sshSession.setConfig(sshConfig);
            // 开启session连接
            sshSession.connect(1500);
            // 获取sftp通道
            sftp = (ChannelSftp) sshSession.openChannel("sftp");
            // 开启
            sftp.connect();
            if (remotePath != null && !"".equals(remotePath)) {
                sftp.cd(remotePath);
            }
            FileOutputStream output = new FileOutputStream(localPath);
            sftp.get(downloadFileName, output);
            output.close();
            sftp.disconnect();
            sshSession.disconnect();
            System.out.println("================下载成功！==================");
        } catch (SftpException | FileNotFoundException | JSchException e) {
            System.out.println("e = " + e);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
